package xyz.constasj.devicejsongenerator;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.*;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MainActivity extends AppCompatActivity {

    public static String getKernelVersion() {
        String kernelVersion = "";
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("/proc/version");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return kernelVersion;
        }
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream), 8 * 1024);
        String info = "";
        String line = "";
        try {
            while ((line = bufferedReader.readLine()) != null) {
                info += line;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedReader.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            if (info != "") {
                final String keyword = "version ";
                int index = info.indexOf(keyword);
                line = info.substring(index + keyword.length());
                index = line.indexOf(" ");
                kernelVersion = line.substring(0, index);
            }
        } catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
        }

        return kernelVersion;
    }

    private int[] parseIP(int ip) {
        int[] ipa = new int[4];
        ipa[0] = ip & 0xFF;
        ipa[1] = (ip >> 8) & 0xFF;
        ipa[2] = (ip >> 16) & 0xFF;
        ipa[3] = (ip >> 24) & 0xFF;
        return ipa;
    }
    public static String stringToMD5(String plainText) {
        byte[] secretBytes = null;
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(
                    plainText.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("没有这个md5算法！");
        }
        String md5code = new BigInteger(1, secretBytes).toString(16);
        for (int i = 0; i < 32 - md5code.length(); i++) {
            md5code = "0" + md5code;
        }
        return md5code;
    }
    private String generateDeviceJSON() {
        deviceJSON JSONObj = new deviceJSON();
        WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiInfo wi = wm.getConnectionInfo();
        TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
        JSONObj.display = Build.DISPLAY;
        JSONObj.product = Build.PRODUCT;
        JSONObj.device = Build.DEVICE;
        JSONObj.brand = Build.BRAND;
        JSONObj.model = Build.MODEL;
        JSONObj.finger_print = Build.FINGERPRINT;
        JSONObj.boot_id = Build.ID;
        JSONObj.proc_version = getKernelVersion();
        JSONObj.protocol = 1;
        JSONObj.imei = tm.getDeviceId();
        JSONObj.brand = Build.BRAND;
        JSONObj.bootloader = Build.BOOTLOADER;
        JSONObj.base_band = "";
        JSONObj.version.incremental = Build.VERSION.INCREMENTAL;
        JSONObj.version.release = Build.VERSION.RELEASE;
        JSONObj.version.codename = Build.VERSION.CODENAME;
        JSONObj.version.sdk = Build.VERSION.SDK_INT;
        JSONObj.sim_info = tm.getSimOperatorName();
        JSONObj.os_type = "android";
        JSONObj.mac_address = wi.getMacAddress();
        int ipAdd = wi.getIpAddress();
        JSONObj.ip_address = parseIP(ipAdd);
        JSONObj.wifi_bssid = wi.getBSSID();
        JSONObj.wifi_ssid = wi.getSSID();
        JSONObj.imsi_md5 = stringToMD5(tm.getSubscriberId());
        JSONObj.android_id = Settings.System.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
        JSONObj.apn = "wifi";
        JSONObj.vendor_name = Build.MANUFACTURER;
        JSONObj.vendor_os_name = System.getProperty("os.name");
        return new Gson().toJson(JSONObj);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            String JSON=generateDeviceJSON();
            super.onCreate(savedInstanceState);
            suentVper.onCreate(savedInstanceState);
            setContiew(R.layout.activity_main);
            TextView JSONView=(TextView)findViewById(R.id.JSONBoard);
            JSONView.setText(JSON);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}